package controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.EventDAO;
import model.MessageDAO;
import model.UserDAO;
import beans.CommentBean;
import beans.EventBean;
import beans.MessageBean;
import beans.UserBean;


@WebServlet("/EventServlet")
public class EventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	public EventServlet() {
		super();
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{	    
			HttpSession session = request.getSession();

			EventBean event = new EventBean();
			event.setHost(request.getParameter("host"));
			String start = request.getParameter("start");
			String starttime = start.substring(0, 4) + "-" + start.substring(4, 6) + "-" + start.substring(6, 8) + " " + start.substring(8, 10) + ":" + start.substring(10, 12) + ":" + start.substring(12);
			Timestamp startts = new Timestamp(0);
			startts = Timestamp.valueOf(starttime);
			event.setStartTimeStamp(startts);
			event = EventDAO.getEvent(event);
			event.setGuestList(EventDAO.getGuests(event));
			UserBean host = new UserBean();
			host.setEmail(event.getHost());
			host = UserDAO.visit(host);
			ArrayList<CommentBean> eventComments = MessageDAO.getEventComments(event);
			UserBean user = (UserBean)session.getAttribute("currentSessionUser");
			event.setNotification(false);
			if (event.isFinished()) {
				if (!event.getGuestList().isEmpty()) {
					event = EventDAO.getEventsRatings(user, event);
					if (event.getHost().equals(user.getEmail()))
						for (UserBean guest : event.getGuestList()) {
							if (guest.getHostToGuestRating() == 0)
								event.setNotification(true);
						}
					else
						for (UserBean guest : event.getGuestList()) {
							if (guest.getEmail().equals(user.getEmail()))
								if (guest.getGuestToHostRating() == 0)
									event.setNotification(true);
						}
				}
			}
			int hostedCount = 0;
			int hostedRatedCount = 0;
			int attendedRatedCount = 0;
			double hostedSum = 0;
			double thisEventGuestSum = 0;
			int thisEventGuestCount = 0;
			ArrayList<EventBean> usersevent = EventDAO.getUsersEvents(host);
			for (EventBean eventt : usersevent) {
				if (eventt.isFinished()) {
					eventt.setGuestList(EventDAO.getGuests(eventt));
					EventBean eventduplo = EventDAO.getEventsRatingsForStats(host, eventt);
					if (eventduplo.getHost().equals(host.getEmail())) {
						hostedCount++;
						for (UserBean guest : eventduplo.getGuestList()) {
							if (guest.getGuestToHostRating() != 0) {
								thisEventGuestSum += guest.getGuestToHostRating();
								thisEventGuestCount++;
							}
						}
						if (thisEventGuestCount != 0) {
							hostedRatedCount++;
							hostedSum += (thisEventGuestSum/thisEventGuestCount);
						}
					}
				}
			}
			host.setTotalHosted(hostedCount);
			if (hostedRatedCount != 0)
				host.setHostRatingAvg(hostedSum/hostedRatedCount);
			else
				user.setHostRatingAvg(0.00);
			session.setAttribute("currentEvent", event);
			session.setAttribute("eventhost", host);
			session.setAttribute("eventcomments", eventComments);
			response.sendRedirect("event.jsp");

		} 


		catch (Throwable theException) 	    
		{
			System.out.println(theException); 
		}
	}	


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
